<!--
 * @Description: 工程列表
 * @Author: charles
 * @Date: 2021-12-14 22:06:30
 * @LastEditors: charles
 * @LastEditTime: 2021-12-14 22:09:44
-->
<template>
  <!-- template模板中用于编写html代码 -->
  <div>
    <!-- 按钮区域 -->
    <div>
      <!-- 新增按钮 -->
      <el-button type="primary" size="small" @click="openHandler">添加</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="tableData.list" style="width: 100%" size="small">
        <el-table-column prop="name" label="工程名称"> </el-table-column>
        <el-table-column prop="serial_number" label="设备编号" width="120">
        </el-table-column>
        <el-table-column
          prop="type"
          label="工程类型"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="charge_name"
          label="区域经理"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="customer.realname"
          label="客户姓名"
          width="120px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="customer.telephone"
          label="客户电话"
          width="120px"
          align="center"
        >
        </el-table-column>
        <el-table-column label="工程状态" width="80" align="center">
          <template v-slot="scope">
            <el-tag size="mini">{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template v-slot="scope">
            <el-button type="text" @click="toEditHandler(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" @click="toBindHandler(scope.row)"
              >绑定</el-button
            >
            <el-button type="text" @click="toEditHandler(scope.row)"
              >结束</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 模态框区域 -->
    <el-dialog 
      @close="dialogCloseHandler"
      width="40%" 
      :title="title" 
      :visible.sync="visible"
    >
      <el-form ref="engineerForm" :model="form" :rules="rules">
        <el-form-item 
          prop="serial_number" 
          label="工程编号" 
          :label-width="formLabelWidth">
          <el-input v-model="form.serial_number"></el-input>
        </el-form-item>
        <el-form-item 
          prop="name"
          label="工程名称" 
          :label-width="formLabelWidth">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item 
          prop="address"
          label="工程地址" 
          :label-width="formLabelWidth">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item 
          prop="charge_id"
          label="工程地址" 
          :label-width="formLabelWidth">
          <el-input v-model="form.charge_id"></el-input>
        </el-form-item>
        <el-form-item 
          prop="type"
          label="工程类型" 
          :label-width="formLabelWidth">
          <el-input v-model="form.type"></el-input>
        </el-form-item>
        <el-form-item 
          prop="latitude"
          label="工程经度" 
          :label-width="formLabelWidth">
          <el-input v-model="form.latitude"></el-input>
        </el-form-item>
        <el-form-item
          prop="longitude" 
          label="工程纬度" 
          :label-width="formLabelWidth">
          <el-input v-model="form.longitude"></el-input>
        </el-form-item>
        <el-form-item 
          prop="customer_name"
          label="客户名称" 
          :label-width="formLabelWidth">
          <el-input v-model="form.customer_name"></el-input>
        </el-form-item>
        <el-form-item 
          prop="customer_telephone"
          label="客户电话" 
          :label-width="formLabelWidth">
          <el-input v-model="form.customer_telephone"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="commitHandler">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页组件 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="parseInt(tableData.page)"
      :page-size="parseInt(tableData.pageSize)"
      layout="total, sizes, prev, pager, next"
      :total="tableData.total"
      :page-sizes="[5, 10, 15, 20]"
    >
    </el-pagination>
  </div>
</template>

<script>
import { get } from "@/utils/request";
export default {
  data() {
    return {
      name: "zhangsan",
      tableData: {},
      visible: false,
      form: {},
      formLabelWidth: "80px",
      title: "",
      params: {
        page: 1,
        pageSize: 10,
      },
      rules: {
        serial_number: [
          { required: true, message: "请输入项目编号", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入项目名称", trigger: "blur" }],
        type: [{ required: true, message: "请输入项目类型", trigger: "blur"}],
        charge_id: [{ required: true, message: "请输入负责人ID", trigger: "blur"}],
        customer_name: [{ required: true, message: "请输入客户名称", trigger: "blur"}],
        customer_telephone: [{ required: true, message: "请输入客户电话", trigger: "blur"}],
      },
    };
  },
  methods: {
    // pageSize改变处理函数
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      // 重载数据
      this.pageQueryEngineering();
    },
    // page改变处理函数
    handleCurrentChange(page) {
      this.params.page = page;
      // 重载数据
      this.pageQueryEngineering();
    },
    // 分页查询设备数据
    pageQueryEngineering() {
      // 使用axios封装的get方法，发送http请求
      get("/engineer/pageQuery", this.params).then((res) => {
        // 将后台接口返回的结果，赋值给tableData
        this.tableData = res.data;
      });
    },
    openHandler() {
      // 置空表单
      this.form = {};
      // 先设置模态框的标题
      this.title = "添加工程";
      // 最后打开模态框
      this.visible = true;
    },
    
    dialogCloseHandler(){
      //clear the form after the dialog closed
      this.$refs["engineerForm"].resetFields();
    },
    
    // 表单确定按钮点击事件
    commitHandler() {
      //validation before commit
      this.$refs["engineerForm"].validate((valid) => {
        if(valid){
          postJSON("/engineer/saveOrUpdate", this.form).then((res) => {
            // 操作成功之后的业务处理
            // 刷新数据
            this.pageQueryEngineering();
            // 提示成功信息
            this.$message({
              message: "操作成功",
              type: "success",
            });
            // 关闭模态框
            this.visible = false;
          });
        } else {
          //faild to pass validation
          return false;
        }
      })
    },
    // 编辑按钮点击事件
    toEditHandler(row) {
      // 将当前行数据中，设备的名称以及编号，设置到表单中
      this.form = {
        id: row.id,
        name: row.name,
        serial_number: row.serial_number,
      };
      // 修改模态框标题
      this.title = "修改设备";
      // 打开模态框
      this.visible = true;
    },

  },
  created() {
    // 在vue的created生命周期阶段，调用查询设备的方法
    this.pageQueryEngineering();
  },
  
};
</script>
<style scoped>
/* scoped标识当前样式表是有局部作用于的，为了防止样式的全局污染 */
</style>